<?php

/*
 * Tomato Framework
 * 
 * Copyright 2012-2012 Luax.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @link http://www.tomatoframework.com
 * @link http://www.luaxlou.com 
 * @version $Id: Dispather.class.php 20 2012-03-17 13:25:46Z luaxcn@gmail.com $
 */

/**
 * MVC DispatherProxy
 * 
 * @package tomatoframework.mvc
 * @author Luax <luaxcn@gmail.com>
 */
class Dispather {

    public static function dispath() {

        set_error_handler('dispather_error_handler', E_ALL ^ E_NOTICE);
        set_exception_handler('dispather_exception_handler');
        $rest = Rest::get();


        $methoName = '_' . $rest['action'];
        $actionName = $rest['action'];
        $resourceName = $rest['resource'];

        $id = $rest['id'];

        $resource = Load::resource($resourceName);

        if ($resource == null) {
            $resource = Load::resource('default');
        }


        if ($id > 0) {
            $result = $resource->$methoName($id);
        } else {
            $result = $resource->$methoName();
        }


        $type = $rest['type'];

        switch ($type) {
            case 'json':
                exit(json_encode($result));
                break;
            case 'xml':
                //@todo parse xml
                break;
            default:

                switch ($actionName) {
                    case 'update':

                        Dispather::redirect('/' . $resourceName . '/show/' . $id, 'update resource successfully.');
                        break;
                    case 'save':

                        Dispather::redirect('/' . $resourceName . '/show/' . $result, 'create resource successfully.');
                        break;
                    case 'delete':

                        Dispather::redirect('/' . $resourceName, 'delete resource successfully.');
                        break;
                    default :
                        $smarty = SmartyAdapter::instance();

                        $message = null;


                        if (isset($_POST['message'])) {
                            $message = $_POST['message'];
                            $smarty->assign('msgtype', $_POST['msgtype']);
                        }

                        $smarty->assign('message', $message);

                        $smarty->assign('template', $resourceName . '/' . $actionName . '.html');
                        $smarty->assign($result);

                        $smarty->display('layout.html',$resourceName.'_'.$actionName);
                        break;
                }


                break;
        }
    }
 

    public static function redirect($url, $message, $msgType = 'success') {


        if ($url == $_SERVER['REQUEST_URI']) {
            //donoting
            return;
        }

        $postData = urlencode(serialize($_POST));

        $message = urlencode($message);
        $url = urlencode($url);

        header('Location: /redirect.php?url=' . $url . '&postdata=' . $postData . '&message=' . $message . '&msgtype=' . $msgType);
        exit();
    }

}

function dispather_error_handler($errno, $errstr, $errfile, $errline) {
    //@todo write code here.
    echo $errstr;
   // exit();
}

function dispather_exception_handler($e) {
    //@todo write code here.
    echo $e->getMessage();
   // exit();
}

?>